/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.web.action;

import org.displaytag.properties.SortOrderEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class ExtendedPaginatedListImpl<T> implements ExtendedPaginatedList<T> {

    private List<T> list = new ArrayList<T>();
    private int pageNumber;
    private int objectsPerPage = PaginatedBaseActionBean.DEFAULT_OBJECTS_PER_PAGE;
    private String sortCriterion;
    private SortOrderEnum sortDirection;
    private int fullListSize;

    public ExtendedPaginatedListImpl() {
    }

    public ExtendedPaginatedListImpl(ExtendedPaginatedList paginatedList) {
        this.list = paginatedList.getList();
        this.pageNumber = paginatedList.getPageNumber();
        this.objectsPerPage = paginatedList.getObjectsPerPage();
        this.sortCriterion = paginatedList.getSortCriterion();
        this.sortDirection = paginatedList.getSortDirection();
        this.fullListSize = paginatedList.getFullListSize();
    }


    public List<T> getList() {
        return list;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public int getObjectsPerPage() {
        return objectsPerPage;
    }

    public int getFullListSize() {
        return fullListSize;
    }

    public String getSortCriterion() {
        return sortCriterion;
    }

    public SortOrderEnum getSortDirection() {
        return sortDirection == SortOrderEnum.ASCENDING ? SortOrderEnum.DESCENDING : SortOrderEnum.ASCENDING;
    }



    public String getSearchId() {
        return null;
    }




    public void setList(List<T> list) {
        this.list = list;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setFullListSize(int fullSize) {
        this.fullListSize = fullSize;
    }

    public void setSortCriterion(String sortCriterion) {
        this.sortCriterion = sortCriterion;
    }

    public void setSortDirection(SortOrderEnum sortDirection) {
        this.sortDirection = sortDirection;
    }

    public void setObjectsPerPage(int objectsPerPage) {
        this.objectsPerPage = objectsPerPage;
    }

    public int getTotalPages() {
        if (getFullListSize() <= 0 || getObjectsPerPage() <= 0) {
            return 0;
        }
        int total = getFullListSize() / getObjectsPerPage();
        int remainder = getFullListSize() % getObjectsPerPage();
        if (remainder > 0) {
            return total + 1;
        }
        return total;
    }

    public boolean isAnyPreviousPage() {
        return getPageNumber() > 1;
    }

    public boolean isAnyNextPage() {
        return getPageNumber() < getTotalPages();
    }

}
